﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace $rootnamespace$.$subnamespace$
{
    public interface I$safeitemrootname$
    {
        void AddModel(I$subnamespace$Model view);
        void AddView(I$subnamespace$View view);
    }

    /// <summary>
    /// A controller is the link between a user and the system. It provides the user with input by
    /// arranging for relevant views to present themselves in appropriate places on the screen. It
    /// provides means for user output by presenting the user with menus or other means of giving
    /// commands and data. The controller receives such user output, translates it into the appropriate
    /// messages and pass these messages on .to one or more of the views.
    /// A controller should never supplement the views, it should for example never connect the
    /// views of nodes by drawing arrows between them.
    /// Conversely, a view should never know about user input, such as mouse operations and
    /// keystrokes. It should always be possible to write a method in a controller that sends messages
    /// to views which exactly reproduce any sequence of user commands.
    /// </summary>
    public class $safeitemrootname$ : I$safeitemrootname$
    {
        /// <summary>
        /// Models that are part of to this controller
        /// </summary>
        public List<I$subnamespace$Model> Models { get; private set; }

        /// <summary>
        /// Views that are part of to this controller
        /// </summary>
        public List<I$subnamespace$View> Views { get; private set; }

        public $subnamespace$Controller(I$subnamespace$Model model, I$subnamespace$View view)
        {
            InitController();

            // Add the model
            AddModel(model);

            // Add the view
            AddView(view);
        }

        public $subnamespace$Controller(List<I$subnamespace$Model> modelList, List<I$subnamespace$View> viewList)
        {
            InitController();

            // Add the models that are part of this controller
            foreach (var model in modelList) AddModel(model);

            // Add the views that are part of this controller
            foreach (var view in viewList) AddView(view);
        }

        void InitController()
        {
            Models = new List<I$subnamespace$Model>();
            Views = new List<I$subnamespace$View>();
        }

        /// <summary>
        /// Add a model to this controller
        /// </summary>
        /// <param name="view"></param>
        public void AddModel(I$subnamespace$Model model)
        {
            model.AddToController(this);
            Models.Add(model);
        }

        /// <summary>
        /// Add a view to this controller
        /// </summary>
        /// <param name="view"></param>
        public void AddView(I$subnamespace$View view)
        {
            // Ask view before subscribing as its new controller
            if (!view.Subscribe(this)) return;

            // Add the view to this controller
            Views.Add(view);

            // Create UI events for the controller from the view
            AddUiEvents(view);
        }

        /// <summary>
        /// The controller can handle as many events as you want to. 
        /// Different views may subscribe to different events
        /// </summary>
        void AddUiEvents(I$subnamespace$View view)
        {
            // Example of an event that a view of the type $subnamespace$View can subscribe to
            if (view is $subnamespace$View)
                (view as $subnamespace$View).SubmitButton.Click += new EventHandler($subnamespace$SubmitButtonClick);
        }

        /// <summary>
        /// Example event that is handled by the controller and passed thru to the view event reciever
        /// </summary>
        /// <param name="sender">Control that was clicked</param>
        /// <param name="e">Event arguments</param>
        void $subnamespace$SubmitButtonClick(object sender, EventArgs e)
        {
            // The controller calls the correct model event reciever for that type of model
            foreach (var model in Models.OfType<$subnamespace$Model>()) model.GetUserTasks();
        }


    }
}
